<?php
/**
 * Project: AIGC
 * Author: moufer <moufer@163.com>
 * Date: 2024/9/21 00:09
 */

namespace module\document\front\form;

use module\document\enum\DocumentPageType;
use module\document\enum\DocumentTypeEnum;
use mof\enum\StatusEnum;
use mof\front\Form;
use mof\Model;

class DocumentForm extends Form
{
    protected array $validate = [
        'param' => [
            'type', 'sub_type', 'title', 'content/s,html', 'status/d',
        ],
        'rule'  => [
            'title|标题'      => 'require|length:2,100',
            "type|类型"       => 'require',
            "sub_type|子类型" => 'requireIf:type,page|unique:document,type^sub_type',
            'status|状态'     => 'require|between:0,2',
            'content|内容'    => 'require|length:10,50000',
        ],
    ];

    protected function dialogAttrs(Model $model = null): array
    {
        $result = parent::dialogAttrs($model);
        $result['width'] = '70%';
        $result['beforeClose'] = [
            'check'   => ['content'], //对比字段
            'message' => '您编辑的内容尚未保存，是否放弃编辑？',
        ];
        //$result['fullscreen'] = true;
//        $result['close-on-click-modal'] = false;
//        $result['close-on-press-escape'] = false;
        //$result['align-center'] = true;
        return $result;
    }

    protected function elements(Model $model = null): array
    {
        return [
            [
                "label"   => "类型",
                "prop"    => "type",
                "value"   => $model['type'] ?? '',
                "type"    => "select",
                "options" => DocumentTypeEnum::toDict()->toElementData()->toSelectOptions(),
                "colSpan" => 12,
            ],
            [
                "label"    => "子类型",
                "prop"     => "sub_type",
                "value"    => $model['sub_type'] ?? '',
                "type"     => "select",
                "options"  => DocumentPageType::toDict()->toElementData()->toSelectOptions(),
                "_visible" => "type=" . DocumentTypeEnum::PAGE->value,
                "colSpan"  => 12,
            ],
            [
                "prop"    => "title",
                "label"   => "标题",
                "value"   => $model ? $model->getAttr('title') : '',
                "intro"   => "标题字数不超过100个字符。",
                "colSpan" => 17,
            ],
            [
                "label"   => "状态",
                "prop"    => "status",
                "value"   => $model['status'] ?? 1,
                "type"    => "select",
                "options" => StatusEnum::toDict()->toElementData()->toSelectOptions(),
                "colSpan" => 7,
            ],
            [
                "prop"   => "content",
                "label"  => "内容",
                "value"  => $model['content'] ?? '',
                'type'   => 'editor',
                'config' => [
                    'autoHeightEnabled'  => false,
                    'initialFrameHeight' => 400,
                    'initialFrameWidth'  => '99.8%',
                ],
            ],
        ];
    }
}


